package com.hzgj.soa.spring;

import com.google.common.collect.Maps;
import com.hzgj.bcl.util.lang.StrKit;
import com.hzgj.soa.spring.schema.SoaNamespaceHandler;
import lombok.Getter;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Map;

/**
 * Created by warning5 on 17-7-7.
 */
public class DomainConfigBean implements ApplicationContextAware, InitializingBean {

    private ApplicationContext applicationContext;
    private Map<String, Domain> domains;
    @Getter
    private ReferenceConfig globalReferenceConfig;
    @Getter
    private ServiceConfig globalServiceConfig;

    public Domain getDomain(String name) {
        return domains.get(name);
    }

    @Override
    public void afterPropertiesSet() throws Exception {

        if (SoaNamespaceHandler.domainNames != null) {
            domains = Maps.newHashMap();
            for (String name : SoaNamespaceHandler.domainNames) {
                domains.put(name, applicationContext.getBean(name, Domain.class));
            }
        }
        if(SoaNamespaceHandler.globalReferenceConfig!=null){
            globalReferenceConfig = applicationContext.getBean(SoaNamespaceHandler.globalReferenceConfig, ReferenceConfig.class);
        }
        if(SoaNamespaceHandler.globalServiceConfig!=null){
            globalServiceConfig = applicationContext.getBean(SoaNamespaceHandler.globalServiceConfig, ServiceConfig.class);
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

}
